﻿using System;
using fastCSharp;

namespace diantou.web
{
    /// <summary>
    /// 公共基本调用
    /// </summary>
    [fastCSharp.code.cSharp.webCall(IsPool = true)]
    class index : fastCSharp.code.cSharp.webCall.call<index>
    {
        /// <summary>
        /// 考试域名
        /// </summary>
        internal static subArray<byte> ExamDomain = new subArray<byte>(diantou.dataModel.path.domain.Default.ExamDomain.getBytes());
        /// <summary>
        /// 首页重定向
        /// </summary>
        [fastCSharp.code.cSharp.webCall(FullName = "")]
        public void Load()
        {
            if (requestHeader.Host.simpleEqual(ref ExamDomain)) NotFound404();
            else
            {
                createResponse();
                diantou.dataAccess.user user = pub.GetSessionUser(this);
                if (user == null)
                {
                    location(IsCookie(pub.IndexLoginCookieName) ? diantou.dataModel.path.pub.Index : diantou.dataModel.path.pub.Index);
                    //location(IsCookie(pub.IndexLoginCookieName) ? diantou.dataModel.path.pub.Index : diantou.dataModel.path.pub.NewLogin);
                }
                else location((byte)user.Type < (byte)dataModel.user.type.未审核企业 ? user.Path.NewFocus : diantou.dataModel.path.pub.Default.EnterpriseIndex);
            }
        }
        ///// <summary>
        ///// 请求头部输出
        ///// </summary>
        ////[fastCSharp.code.cSharp.webCall]
        //public void RequestHeader()
        //{
        //    log.Default.Add(requestHeader.Data.ToArray().deSerialize(), new System.Diagnostics.StackFrame(), true);
        //    location(diantou.dataModel.path.pub.Index);
        //}
        /// <summary>
        /// 重置密码验证
        /// </summary>
        /// <param name="email"></param>
        /// <param name="code"></param>
        [fastCSharp.code.cSharp.webCall]
        public void GetPassword(string email, string code, bool isEnterprise)
        {
            diantou.dataAccess.tcpCall.user.getPassword(email, code, idType =>
            {
                keyValue<int, diantou.dataModel.user.type> value = idType.Value;
                if (value.Key == 0) location(diantou.dataModel.path.pub.Index);
                else
                {
                    createResponse();
                    SetSession<webConfig.session>(new webConfig.session { Id = value.Key });
                    location(value.Key, value.Value, isEnterprise);
                }
            });
        }
        /// <summary>
        /// 注册邮件验证
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="code"></param>
        [fastCSharp.code.cSharp.webCall]
        public void Register(int userId, string code, bool isEnterprise)
        {
            diantou.dataAccess.tcpCall.user.register(userId, code, value => location(userId, value.Value, isEnterprise));
        }
        /// <summary>
        /// 用户重定向
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="type"></param>
        private void location(int userId, diantou.dataModel.user.type type, bool isEnterprise)
        {
            switch (type)
            {
                case diantou.dataModel.user.type.程序员:
                    location(new diantou.dataModel.path.user { Id = userId }.NewFocus);
                    return;
                case diantou.dataModel.user.type.未审核企业:
                    location(diantou.dataModel.path.pub.Default.EnterpriseLoginError);
                    return;
                case diantou.dataModel.user.type.未认证企业:
                case diantou.dataModel.user.type.已认证企业:
                    location(diantou.dataModel.path.pub.Default.EnterpriseIndex);
                    return;
                default:
                    location(isEnterprise ? diantou.dataModel.path.pub.EnterpriseLogin : diantou.dataModel.path.pub.Index);
                    return;
            }
        }
        /// <summary>
        /// 第三方登录邮件验证
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="code"></param>
        [fastCSharp.code.cSharp.webCall]
        public void SetEmail(int userId, string code)
        {
            diantou.dataAccess.tcpCall.user.setEmailVerify(userId, code, value =>
            {
                if (value.Value)
                {
                    createResponse();
                    SetSession<webConfig.session>(new webConfig.session { Id = userId });
                    location(new diantou.dataModel.path.user { Id = userId }.Setting + "?scroll=passwordScroll");
                }
                else location(diantou.dataModel.path.pub.Index);
            });
        }
    }
}
